/*
 * Copyright (c) 2010-2011 Josef Hardi <josef.hardi@gmail.com>
 * Copyright (c) 2010 Piotr Zagawa <piotr.zagawa@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package id.duapuluhdua.dakidaki.data.items;

import java.util.Calendar;

import android.database.Cursor;

import id.duapuluhdua.dakidaki.data.common.DataItem;
import id.duapuluhdua.dakidaki.data.tables.DataTableTracks;

/**
 *
 */
public class DataItemTrack extends DataItem
{
  /** Fields */
  private String mName;
  private String mDescription;

  private long mStartTime;
  private long mEndTime;
  private long mTotalTime;
  private long mTotalDistance;

  private String mUID;
  private int mType;

  private boolean bClosed;

  public DataItemTrack()
  {
    clear();
  }

  public DataItemTrack(DataItemTrack item)
  {
    super.id = item.id;

    mName = item.mName;
    mDescription = item.mDescription;

    mStartTime = item.mStartTime;
    mEndTime = item.mEndTime;
    mTotalTime = item.mTotalTime;
    mTotalDistance = item.mTotalDistance;

    mUID = item.mUID;
    mType = item.mType;

    bClosed = item.bClosed;

    mDistance = item.mDistance;
  }

  public DataItemTrack(Cursor cr)
  {
    super.id = cr.getLong(DataTableTracks.column.ID);

    mName = cr.getString(DataTableTracks.column.Name);
    mDescription = cr.getString(DataTableTracks.column.Description);

    mStartTime = cr.getLong(DataTableTracks.column.StartTime);
    mEndTime = cr.getLong(DataTableTracks.column.EndTime);
    mTotalTime = cr.getLong(DataTableTracks.column.TotalTime);
    mTotalDistance = cr.getLong(DataTableTracks.column.TotalDistance);

    mUID = cr.getString(DataTableTracks.column.UID);
    mType = cr.getInt(DataTableTracks.column.Type);

    bClosed = (cr.getInt(DataTableTracks.column.Closed) == 1);
  }

  public void clear()
  {
    mName = "";
    mDescription = "";

    mStartTime = 0;
    mEndTime = 0;
    mTotalTime = 0;
    mTotalDistance = 0;

    mUID = "";
    mType = 0;

    bClosed = false;
  }

  public String getName()
  {
    return mName;
  }

  public int getType()
  {
    return mType;
  }

  /**
   * @return
   */
  public boolean isClosed()
  {
    return bClosed;
  }

  public long getTotalTimeWhenPauseInSeconds()
  {
    final long timeNowMs = Calendar.getInstance().getTimeInMillis();
    return mTotalTime + (timeNowMs - mEndTime);
  }

  public int getTotalTimeInSeconds()
  {
    return (int)(mTotalTime / 1000.0f);
  }
}
